package com.yqb.study.common.mq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.yqb.study.config.mq.MqConfig;
import com.yqb.study.modular.persistence.dao.SeckillMapper;

@Component
public class MqReceiver {

	private static Logger log = LoggerFactory.getLogger(MqReceiver.class);

	// @Autowired
	// GoodsService goodsService;
	//
	// @Autowired
	// OrderService orderService;
	//
	// @Autowired
	// MiaoshaService miaoshaService;

	@Autowired
	private SeckillMapper seckillMapper;
	
	// --------------------- fanout ---------------------
	@RabbitListener(queues = MqConfig.FANOUT_QUEUE1)
	public void receive1FromFanoutQueue1(String message) {
		log.info("receive1 fanout queue1 message:" + message);
	}

	@RabbitListener(queues = MqConfig.FANOUT_QUEUE2)
	public void receive1FromFanoutQueue2(String message) {
		log.info("receive1 fanout queue2 message:" + message);
	}	
	// --------------------- fanout ---------------------	
	
	// --------------------- direct ---------------------
	@RabbitListener(queues = MqConfig.DIRECT_QUEUE1)
	public void receive1FromDirectQueue1(String message) {
		log.info("receive1 direct queue1 message:" + message);
	}

	@RabbitListener(queues = MqConfig.DIRECT_QUEUE2)
	public void receive1FromDirectQueue2(String message) {
		log.info("receive1 direct queue2 message:" + message);
	}	
	// --------------------- direct ---------------------	

	// --------------------- topic ---------------------
	@RabbitListener(queues = MqConfig.TOPIC_QUEUE1)
	public void receive1FromTopicQueue1(String message) {
		log.info("receive1 topic queue1 message:" + message);
	}
	
	@RabbitListener(queues = MqConfig.TOPIC_QUEUE1)
	public void receive2FromTopicQueue1(String message) {
		// 同一个topic队列好像只有一个能收到消息
		log.info("receive2 topic queue1 message:" + message);
	}	
	
	@RabbitListener(queues = MqConfig.TOPIC_QUEUE2)
	public void receive1FromTopicQueue2(String message) {
		log.info("receive1 topic queue2 message:" + message);
	}	
	// --------------------- topic ---------------------

	// @RabbitListener(queues=MqConfig.MIAOSHA_QUEUE)
	// public void receiveMqMessage(String message) {
	// log.info("receive message:"+message);
	// MqMessage mm = FastJsonUtil.jsonToObject(message, MqMessage.class);
	// User user = mm.getUser();
	// long seckillId = mm.getSeckill().getSeckillId();
	//
	// Seckill entity = new Seckill();
	// entity.setSeckillId(seckillId);
	// log.info("秒杀的商品是{}", seckillMapper.selectOne(entity));
	//
	//// GoodsVo goods = goodsService.getGoodsVoByGoodsId(goodsId);
	//// int stock = goods.getStockCount();
	//// if(stock <= 0) {
	//// return;
	//// }
	//// //判断是否已经秒杀到了
	//// MiaoshaOrder order =
	// orderService.getMiaoshaOrderByUserIdGoodsId(user.getId(), goodsId);
	//// if(order != null) {
	//// return;
	//// }
	//// //减库存 下订单 写入秒杀订单
	//// miaoshaService.miaosha(user, goods);
	// }

	//

	//
	// @RabbitListener(queues=MQConfig.HEADER_QUEUE)
	// public void receiveHeaderQueue(byte[] message) {
	// log.info(" header queue message:"+new String(message));
	// }
	//

}
